package datastructure.ch03_linked.ch10_double_linkedlist;

import datastructure.ch03_linked.ch07_nodes.DoubleNode;
import datastructure.ch03_linked.ch08_interface_abstract.AbstractLinkedList;

/**
 * 双向链表操作
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class DoubleLinkedList<E> extends AbstractLinkedList<E> {
    public static void main(String[] args) {
        DoubleLinkedList<Integer> data = new DoubleLinkedList<>();
        data.add(0, 1);
        data.add(1, 2);
        data.add(2, 3);
        data.add(100);
        data.add(2, 66);
        Integer set = data.set(0, 100);
        data.remove(0);
        System.out.println(set);
        System.out.println(data);
    }

    DoubleNode<E> first;
    DoubleNode<E> last;

    @Override
    public int indexOf(E element) {
        return 0;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        if (index == size) { // 添加第一个元素或尾部添加
            DoubleNode<E> oldNode = last;
            // DoubleNode<E> last相当于又开辟了一个last栈空间
            last = new DoubleNode<>(oldNode, element, null);
            // 尾可能为空
            if (oldNode == null) {
                first = last;
            } else {
                oldNode.next = last;
            }
        } else {
            DoubleNode<E> next = getNode(index);
            DoubleNode<E> prev = next.prev;
            DoubleNode<E> node = new DoubleNode<>(prev, element, next);
            next.prev = node;
            // 头可能为空
            if (prev == null) {  // index == 0
                first = node;
            } else {
                prev.next = node;
            }
        }
        size++;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        DoubleNode<E> node = getNode(index);
        DoubleNode<E> prev = node.prev;
        DoubleNode<E> next = node.next;

        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
        }

        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
        }
        size--;
        return node.element;
    }

    @Override
    public E set(int index, E element) {
        DoubleNode<E> node = getNode(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public E get(int index) {
        return getNode(index).element;
    }

    private DoubleNode<E> getNode(int index) {
        DoubleNode<E> node;
        if (index < (size >> 1)) {
            node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
        } else {
            node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        DoubleNode<E> node = first;
        while (node != null) {
            result.append(node);
            if (node.next != null)
                result.append("->");
            node = node.next;
        }
        return result.toString();
    }
}
